package com.ygj.ticketManage.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;
import org.springframework.transaction.annotation.Transactional;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.entity.AllCondition;
import com.base.common.json.JsonUtil;
import com.cjy.common.bean.Files;
import com.cjy.common.bean.OpAttributes;
import com.ygj.ticketManage.service.TicketManageService;

/**
 * 工单管理action
 * 
 * @author Kevin Colin
 * @datetime 2015年8月4日上午10:51:42
 * 
 */

@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class TicketManageAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	private int employeeId; // 员工id
	private int compoundsId;	// 小区id
	private String employeeIds; // 员工id集合，以,分隔
	private String ticketIds; // 工单id集合，以,分隔
	private String report; // 工单记录报告
	private String distributeInfos;
	private int reportCategories = -1; // 工单进度类型
	private int status; // 工单进度状态
	private int ticketCategories = -1; // 工单类型
	private int ticketStatus = -1;	// 工单状态
	private Date beginTime; // 时间段的开始
	private Date endTime; // 时间段的结束
	private int page; // 当前页数
	private int resourceEmployeeId; // 消息来源员工id
	private int returnVisitScore;

	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosFileName;
	private String[] filePhotosContentType;

	private TicketManageService ticketManageService; // 工单管理service

	public int getReturnVisitScore() {
		return returnVisitScore;
	}

	public void setReturnVisitScore(int returnVisitScore) {
		this.returnVisitScore = returnVisitScore;
	}

	public int getReportCategories() {
		return reportCategories;
	}

	public void setReportCategories(int reportCategories) {
		this.reportCategories = reportCategories;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getTicketCategories() {
		return ticketCategories;
	}

	public void setTicketCategories(int ticketCategories) {
		this.ticketCategories = ticketCategories;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getTicketIds() {
		return ticketIds;
	}

	public void setTicketIds(String ticketIds) {
		this.ticketIds = ticketIds;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public int getResourceEmployeeId() {
		return resourceEmployeeId;
	}

	public void setResourceEmployeeId(int resourceEmployeeId) {
		this.resourceEmployeeId = resourceEmployeeId;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public int getTicketStatus() {
		return ticketStatus;
	}

	public void setTicketStatus(int ticketStatus) {
		this.ticketStatus = ticketStatus;
	}

	public File[] getFilePhotos() {
		return filePhotos;
	}

	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}

	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}

	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}

	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}

	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	public String getDistributeInfos() {
		return distributeInfos;
	}

	public void setDistributeInfos(String distributeInfos) {
		this.distributeInfos = distributeInfos;
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public String getEmployeeIds() {
		return employeeIds;
	}

	public void setEmployeeIds(String employeeIds) {
		this.employeeIds = employeeIds;
	}

	public String getReport() {
		return report;
	}

	public void setReport(String report) {
		this.report = report;
	}

	public TicketManageService getTicketManageService() {
		return ticketManageService;
	}

	public void setTicketManageService(TicketManageService ticketManageService) {
		this.ticketManageService = ticketManageService;
	}

	/**
	 * 根据用户id和工单类型和状态查询工单进度和详细信息
	 * 
	 * @return
	 */
	@Action(value = "getTicketsByCategoriesAndStatus")
	public String getTicketsByCategoriesAndStatus() {
		try {
			AllCondition allCondition = new AllCondition();
			allCondition.setBeginTime(beginTime);
			allCondition.setEndTime(endTime);
			allCondition.setPage(page);
			allCondition.setTicketStatus(ticketStatus);
			allCondition.setReportCategories(reportCategories);
			allCondition.setStatus(status);
			allCondition.setTicketCategories(ticketCategories);

			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add(
					"result",
					ticketManageService.getTicketsByCategoriesAndStatus(
							employeeId, compoundsId, allCondition)));
		} catch (Exception e) {
			 e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 根据用户id和工单类型和状态查询工单条数
	 * 
	 * @return
	 */
	@Action(value = "getTicketsCountsByCategoriesAndStatus")
	public String getTicketsCountsByCategoriesAndStatus() {
		try {
			AllCondition allCondition = new AllCondition();
			allCondition.setBeginTime(beginTime);
			allCondition.setEndTime(endTime);
			allCondition.setPage(page);
			allCondition.setTicketStatus(ticketStatus);
			allCondition.setReportCategories(reportCategories);
			allCondition.setStatus(status);
			allCondition.setTicketCategories(ticketCategories);

			actionResult = ActionResult
					.getActionResult(new OpAttributes()
							.add("code", 1)
							.add("result",
									new OpAttributes()
											.add("totalCounts",
													ticketManageService
															.getTicketsPagesByEmployeeIdAndReportCategoriesAndReportStatus(
																	employeeId, compoundsId,
																	allCondition))));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 分配工单
	 * 
	 * @return
	 */
	@Action(value = "distributeTickets")
	@Transactional
	public String distributeTickets() {
		try {
			int result = ticketManageService.distributeTickets(
					ticketIds.split(","),
					JsonUtil.string2Obejct(distributeInfos, List.class));
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			logger.error(e.fillInStackTrace());
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 提前指定员工给工单
	 * 
	 * @return
	 */
	/*
	 * @Action(value = "distributeEmployeesForTicket")
	 * 
	 * @Transactional public String distributeEmployeesForTicket(){ try { int
	 * result = ticketManageService.distributeEmployeeForTicket(); actionResult
	 * = ActionResult.getActionResult(new OpAttributes().add( "code", result));
	 * } catch (Exception e) { e.printStackTrace(); actionResult =
	 * ActionResult.getActionResult(new OpAttributes().add( "code", 0)); }
	 * return returnType(); }
	 */

	/**
	 * 接单
	 * 
	 * @return
	 */
	@Action(value = "getTicketByMySelf")
	public String getTicketByMySelf() {
		try {
			int result = ticketManageService.getTicketByMySelf(ticketIds
					.split(","));
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	@Action(value = "finishTicketByMySelf", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	@Transactional
	public String finishTicketByMySelf() {
		try {
			Files files = new Files();
			files.setFile(filePhotos);
			files.setFileContentType(filePhotosContentType);
			files.setFileFileName(filePhotosFileName);

			// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
			HttpServletRequest servletRequest = new StrutsRequestWrapper(
					request);
//			System.out.println(report);
			String basePath = null;
			String path = servletRequest.getContextPath();
			basePath = servletRequest.getScheme() + "://"
					+ ConfigConstants.SERVERIP + ":"
					+ servletRequest.getServerPort() + path + "/";

			int result = ticketManageService.finishTicketByMySelf(
					ticketIds.split(","), report, files, basePath, compoundsId, returnVisitScore);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	public static void main(String[] args) {
		
		
		/*String[] arr = str.split("G");
		for(int i = 0; i < arr.length; i++){
			arr[i] += "G";
		}
		
		for(String a : arr){
			System.out.println(a.substring(str.indexOf("H"), str.indexOf("R") + 1));
		}*/
	}
}
